import math, typing

def encode(number: int, endian:typing.Literal['little','big']='big') -> tuple[bytes, int]:
    """ Encodes a number as a \"Dynamic Integer\" """

    number = abs(number)
    out = bytes()
    if number == 0:
        length = 1
    else:
        length = math.ceil(number.bit_length() / 8)
    if length > 255:
        raise ValueError("Number is to big")
    out += length.to_bytes(1, endian)
    out += number.to_bytes(length, endian)
    return out, len(out)

def decode(number: bytes, endian:typing.Literal['little','big']='big') -> tuple[int, int]:
    """ Decodes a number from a \"Dynamic Integer\" """

    out_len = 0
    number_length = number[0]
    out_len += 1
    del number[0]
    number_bytes = bytearray()
    for _ in range(number_length):
        number_bytes.append(number[0])
        out_len += 1
        del number[0]
    out = int.from_bytes(number_bytes, endian)
    return out, out_len
